{% extends "base.html" %}
{% load static %}
{% load score_filters %}

{% block title %}{{ exam_record.exam.name }} - {{ exam_record.subject.name }}成绩详情{% endblock %}

{% block extra_css %}
<link rel="stylesheet" href="{% static 'score/css/score_detail.css' %}">
{% endblock %}

{% block content %}
<div class="container mt-4 mb-5">
    <!-- 页面标题与导航 -->
    <div class="d-flex flex-wrap justify-content-between align-items-center mb-4 page-header">
        <div>
            <h2 class="mb-1">{{ exam_record.exam.name }}</h2>
            <p class="text-muted mb-0">
                考试类型：
                {% if exam_record.exam.exam_type %}
                    {{ exam_record.exam.exam_type.name }}
                {% else %}
                    未知类型
                {% endif %}
                | 考试日期：{{ exam_record.exam.exam_date|date:"Y年m月d日" }}
                | 科目：{{ exam_record.subject.name }}
            </p>
        </div>
        <div class="header-actions">
            <a href="{% url 'score:score_list'  %}" class="btn btn-outline-secondary me-2 action-btn">
                <i class="fas fa-arrow-left"></i> 返回列表
            </a>
            <a href="{% url 'score:score_edit' exam_record.id %}" class="btn btn-primary action-btn">
                <i class="fas fa-edit"></i> 编辑成绩
            </a>
        </div>
    </div>

    <!-- 成绩概览卡片 -->
    <div class="row mb-4">
        <!-- 主要成绩卡片 -->
        <div class="col-lg-8 mb-4">
            <div class="card score-main-card h-100">
                <div class="card-body d-flex flex-column">
                    <div class="row flex-grow-1 align-items-center">
                        <!-- 左侧：成绩显示和进度条 -->
                        <div class="col-md-6 h-100">
                            <div class="score-content-wrapper h-100 d-flex flex-column justify-content-between">
                                <!-- 成绩显示部分 -->
                                <div class="score-display-section">
                                    <h3 class="score-label">{{ exam_record.subject.name }}成绩</h3>
                                    <div class="d-flex align-items-end gap-3 mb-4">
                                        <span class="score-display">{{ exam_record.score }}</span>
                                        <span class="score-full">/{{ full_score }}分</span>
                                    </div>
                                </div>

                                <!-- 进度条部分 -->
                                <div class="score-progress-section">
                                    <div class="progress-info mb-2">
                                        <span class="text-sm">得分率：{{ score_rate }}%</span>
                                        <span class="text-sm text-danger">总失分：{{ total_loss }}分</span>
                                    </div>
                                    <div class="progress" style="height: 16px; border-radius: 10px; background: rgba(0,0,0,0.05);">
                                        <div class="progress-bar" role="progressbar"
                                             data-width="{{ score_rate }}"
                                             style="background: linear-gradient(90deg, {{ exam_record.subject.color }}, {{ exam_record.subject.color }}99); border-radius: 10px;"
                                             aria-valuenow="{{ score_rate }}" aria-valuemin="0" aria-valuemax="100">
                                            <span class="progress-text">{{ score_rate }}%</span>
                                        </div>
                                    </div>
                                </div>
                            </div>
                        </div>

                        <!-- 右侧：环形图 -->
                        <div class="col-md-6 h-100">
                            <div class="chart-container h-100 d-flex align-items-center justify-content-center">
                                <canvas id="scoreRateChart"></canvas>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>

        <!-- 成绩状态卡片 -->
        <div class="col-lg-4 mb-4">
            <div class="card score-status-card h-100">
                <div class="card-body">
                    <h5 class="card-title">成绩状态</h5>
                    <div class="status-content">
                        <div class="status-item">
                            <i class="fas fa-user-graduate"></i>
                            <div>
                                <span class="label">考生</span>
                                <span class="value">
                                    {% if exam_record.student.user.get_full_name %}
                                        {{ exam_record.student.user.get_full_name }}
                                    {% else %}
                                        {{ exam_record.student.user.username }}
                                    {% endif %}
                                </span>
                            </div>
                        </div>
                        <div class="status-item">
                            <i class="fas fa-clock"></i>
                            <div>
                                <span class="label">考试时长</span>
                                <span class="value">{{ exam_record.exam_duration }}分钟</span>
                            </div>
                        </div>
                        <div class="status-item">
                            <i class="fas fa-chart-line"></i>
                            <div>
                                <span class="label">成绩评级</span>
                                <span class="value rating-{{ score_rate|floatformat:0 }}">
                                    {% if score_rate >= 90 %}
                                        优秀
                                    {% elif score_rate >= 80 %}
                                        良好
                                    {% elif score_rate >= 60 %}
                                        合格
                                    {% else %}
                                        不合格
                                    {% endif %}
                                </span>
                            </div>
                        </div>
                        <div class="status-item">
                            <i class="fas fa-comment"></i>
                            <div>
                                <span class="label">备注</span>
                                <span class="value">
                                    {% if exam_record.comment %}
                                        {{ exam_record.comment|truncatechars:20 }}
                                    {% else %}
                                        无备注
                                    {% endif %}
                                </span>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 关键指标卡片 -->
    <div class="row mb-4">
        <div class="col-12">
            <div class="card metrics-card">
                <div class="card-body">
                    <div class="row text-center">
                        <div class="col-md-3 col-6 mb-3">
                            <div class="metric-item">
                                <div class="metric-value text-primary">{{ score_rate }}%</div>
                                <div class="metric-label">得分率</div>
                            </div>
                        </div>
                        <div class="col-md-3 col-6 mb-3">
                            <div class="metric-item">
                                <div class="metric-value text-danger">{{ total_loss }}分</div>
                                <div class="metric-label">总失分</div>
                            </div>
                        </div>
                        <div class="col-md-3 col-6 mb-3">
                            <div class="metric-item">
                                <div class="metric-value text-warning">{{ loss_details|length }}题</div>
                                <div class="metric-label">失分题目</div>
                            </div>
                        </div>
                        <div class="col-md-3 col-6 mb-3">
                            <div class="metric-item">
                                <div class="metric-value text-success">{{ exam_record.exam_duration }}分钟</div>
                                <div class="metric-label">考试时长</div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- 失分明细部分 -->
    <div class="mb-4">
        <div class="d-flex flex-wrap justify-content-between align-items-center mb-3">
            <h3 class="mb-0">失分明细</h3>
            <a href="{% url 'score:loss_detail_add' exam_record.id %}" class="btn btn-success action-btn mt-2 mt-sm-0">
                <i class="fas fa-plus"></i> 添加失分明细
            </a>
        </div>

        <!-- 失分类型统计 -->
        {% if loss_details %}
            <div class="loss-stat-card">
                <h5 class="loss-stat-title">
                    <i class="fas fa-chart-pie"></i> 失分类型统计
                </h5>
                <div class="loss-stat-tags">
                    {% for loss_type, count in loss_type_count.items %}
                        <span class="loss-tag {{ loss_type }}" style="background: {{ loss_type_colors|get_item:loss_type }}; color: {{ loss_type_text_colors|get_item:loss_type }}; border-color: {{ loss_type_border_colors|get_item:loss_type }};">
                            {{ loss_type_names|get_item:loss_type }} <span>{{ count }}次</span>
                        </span>
                    {% endfor %}
                    <span class="loss-tag total">总失分题数 <span>{{ loss_details|length }}题</span></span>
                </div>
            </div>
        {% endif %}

        <!-- 失分明细表格 -->
        {% if loss_details %}
        <div class="table-responsive loss-table">
            <table class="table table-hover mb-0">
                <thead>
                    <tr>
                        <th>题号</th>
                        <th>知识点</th>
                        <th>失分类型</th>
                        <th>失分数值</th>
                        <th>备注</th>
                        <th style="width: 120px;">操作</th>
                    </tr>
                </thead>
                <tbody>
                    {% for detail in loss_details %}
                    <tr>
                        <td class="question-number">{{ detail.question_number }}</td>
                        <td>
                            {% if detail.knowledge_point %}
                                <span class="knowledge-point">{{ detail.knowledge_point.name }}</span>
                            {% else %}
                                {{ detail.custom_knowledge_point|default:"未填写" }}
                            {% endif %}
                        </td>
                        <td>
                            <span class="loss-type-badge {{ detail.loss_type }}"
                                  style="background: {{ detail.loss_color }}; color: white;">
                                {{ detail.get_loss_type_display }}
                            </span>
                        </td>
                        <td class="loss-points">{{ detail.points }}分</td>
                        <td class="loss-description">
                            {{ detail.description|truncatechars:30|default:"无备注" }}
                        </td>
                        <td>
                            <div class="btn-group btn-group-sm">
                                <a href="{% url 'score:loss_detail_edit' detail.id %}" class="btn btn-outline-primary action-btn" title="编辑">
                                    <i class="fas fa-edit"></i>
                                </a>
                                <!-- 新的删除按钮 - 使用简单确认对话框 -->
                                <form method="post" action="{% url 'score:loss_detail_delete' detail.id %}"
                                      style="display: inline;" class="delete-form">
                                    {% csrf_token %}
                                    <button type="submit" class="btn btn-outline-danger action-btn delete-btn"
                                            title="删除" data-question="{{ detail.question_number }}" data-points="{{ detail.points }}">
                                        <i class="fas fa-trash"></i>
                                    </button>
                                </form>
                            </div>
                        </td>
                    </tr>
                    {% endfor %}
                </tbody>
                <tfoot>
                    <tr class="table-light">
                        <td colspan="3" class="text-end"><strong>总失分：</strong></td>
                        <td class="text-danger"><strong>{{ total_loss }}分</strong></td>
                        <td colspan="2"></td>
                    </tr>
                </tfoot>
            </table>
        </div>
        {% else %}
        <div class="alert alert-info p-4 border-0 shadow-sm rounded-8">
            <div class="d-flex align-items-center">
                <i class="fas fa-info-circle text-info fs-4 me-3"></i>
                <div>
                    <h6 class="alert-heading mb-1">暂无失分明细</h6>
                    <p class="mb-0 text-muted">点击"添加失分明细"记录具体失分情况，帮助分析薄弱知识点。</p>
                </div>
            </div>
        </div>
        {% endif %}
    </div>

    <!-- 分析报告部分 -->
    <div class="row gap-4">
        <!-- 知识点失分分析 -->
        {% if similar_records_count > 0 %}
        <div class="col-md-8">
            <div class="card analysis-card h-100">
                <div class="card-header">
                    <h3 class="mb-0 fs-5">知识点失分分析</h3>
                </div>
                <div class="card-body">
                    <div class="mb-3">
                        <p class="text-muted mb-2">
                            该科目下的知识点在过去 <span class="text-primary font-medium">{{ similar_records_count }} 次</span> 考试中，
                            累计出现 <span class="text-danger font-medium">{{ total_similar_loss_count }} 次</span> 失分，
                            平均每次失分为 <span class="text-warning font-medium">{{ avg_similar_loss|floatformat:1 }} 分</span>。
                        </p>
                        <p class="text-muted">
                            其中 <span class="font-medium">{{ top_loss_knowledge }}</span> 是最易失分的知识点，
                            建议重点复习相关内容。
                        </p>
                    </div>
                    <a href="{% url 'score:knowledge_analysis' %}?subject={{ exam_record.subject.id }}" class="btn btn-outline-info action-btn">
                        <i class="fas fa-chart-line"></i> 查看完整分析报告
                    </a>
                </div>
            </div>
        </div>
        {% endif %}

        <!-- 成绩趋势对比 -->
        {% if student_exam_records|length > 1 %}
        <div class="col-md-4">
            <div class="card analysis-card h-100">
                <div class="card-header">
                    <h3 class="mb-0 fs-5">成绩趋势</h3>
                </div>
                <div class="card-body">
                    <div class="chart-container" style="height: 180px;">
                        <canvas id="scoreTrendChart"></canvas>
                    </div>
                    <div class="mt-3 text-center">
                        <p class="text-sm text-muted mb-0">
                            近 {{ student_exam_records|length }} 次考试成绩波动：
                            {% if score_trend == 'up' %}
                                <span class="text-success"><i class="fas fa-arrow-up"></i> 上升</span>
                            {% elif score_trend == 'down' %}
                                <span class="text-danger"><i class="fas fa-arrow-down"></i> 下降</span>
                            {% else %}
                                <span class="text-muted"><i class="fas fa-minus"></i> 平稳</span>
                            {% endif %}
                        </p>
                    </div>
                </div>
            </div>
        </div>
        {% endif %}
    </div>

    <!-- 改进建议卡片 -->
    <div class="row mt-4">
        <div class="col-12">
            <div class="card suggestion-card">
                <div class="card-header">
                    <h3 class="mb-0 fs-5">学习建议</h3>
                </div>
                <div class="card-body">
                    <div class="row">
                        <div class="col-md-6">
                            <div class="suggestion-item">
                                <i class="fas fa-lightbulb text-warning"></i>
                                <div>
                                    <h6>重点复习知识点</h6>
                                    <p class="text-muted mb-0">根据失分情况，建议重点复习以下内容...</p>
                                </div>
                            </div>
                        </div>
                        <div class="col-md-6">
                            <div class="suggestion-item">
                                <i class="fas fa-clock text-info"></i>
                                <div>
                                    <h6>时间管理建议</h6>
                                    <p class="text-muted mb-0">合理分配答题时间，避免时间不足...</p>
                                </div>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block extra_js %}
<script src="{% static 'score/js/score_detail.js' %}"></script>
{% endblock %}